#include <cstdio>
#include <cstring>

const int Limit_Size = 16;
const int Char_Size = 100 + 5;

int opt[ 0xffff ], n, dl[ Limit_Size ], cost[ Limit_Size ], pi[ 0xffff ], total;
char name[ Limit_Size ][ Char_Size ];

void init( )
{
    int i, t;
    scanf("%d", &n);
    t = ( 1 << n ) - 1;
    total = 0;
    for ( i = 0; i < n; i++ )
    {
        scanf("%s%d%d", name[ i ], &dl[ i ], &cost[ i ]);
        total += cost[ i ];
    }
    pi[ 0 ] = -1;
    for ( i = 1; i <= t; i++ )
        opt[ i ] = -1;
    opt[ 0 ] = 0;
}

int find( int con, int sum )
{
    if ( opt[ con ] != -1 )
        return opt[ con ];
    int i, t = 1, p, ex, minc = 0xfffffff, minn;
    for ( i = 0; i < n; i++ )
    {
        if ( t & con )
        {
            p = find( con ^ t, sum - cost[ i ] );
            if ( sum <= dl[ i ] )
                ex = 0;
            else
                ex = sum - dl[ i ];
            if ( minc == p + ex )
                if ( strcmp( name[ minn ], name[ i ] ) < 0 )
                    minn = i;
            if ( minc > p + ex )
            {
                minc = p + ex;
                minn = i;
            }
        }
        t <<= 1;
    }
    pi[ con ] = minn;
    return opt[ con ] = minc;
}

void print( )
{
    int p = ( 1 << n ) - 1, s[ 16 ], t = 0;
    printf("%d\n", find( p, total ) );
    while ( p )
    {
        s[ t++ ] = pi[ p ];
        p = p ^ ( 1 << pi[ p ] );
    }
    while ( t )
        puts( name[ s[ --t ] ] );
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        print( );
    }
    return 0;
}
